﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace etp
{
    public class Program
    {
        static XmlDocument LoadDatabase(string path, Encoding encoding)
        {
            // setup SGMLReader
            var sgmlReader = new Sgml.SgmlReader
                             {
                                 DocType = "QDB",
                                 WhitespaceHandling = WhitespaceHandling.All,
                                 CaseFolding = Sgml.CaseFolding.None,

                                 // COPROCDOE
                                 InputStream = new StringReader("<root>" + File.ReadAllText(path, encoding) + "</root>")
                             };

            // create document
            var doc = new XmlDocument
                      {
                          PreserveWhitespace = true,
                          XmlResolver = null
                      };

            doc.Load(sgmlReader);
            return doc;
        }


        static void Usage()
        {
            Console.WriteLine("etp, The ETest Parser v 0.01 (c) C.c 2011\n");
            Console.WriteLine("usage: etp <input file> <output file> [codepage]");
            Console.WriteLine("\nCodepage is an optional parameter.\nI recomend to use it, if programm sucks a big cock. Usualy it must be 1251.");

        }

        static string CreateHtml(IEnumerable<string> input)
        {
            var template = File.ReadAllText("livesearch.template");
            var script = string.Format("<script type='text/javascript'> var answers = [{0}]</script>",
                                       string.Join(",", input.Select(t => "'"
                                                                          + t.Replace("\r", "")
                                                                                .Replace("\n", "")
                                                                                .Replace("\\", "/")
                                                                                .Replace("'", "\\'")
                                                                          + "'")));

            return script + template;
        }

        static void Main(string[] args)
        {
            // Parsing args
            var encoding = Encoding.Default;
            string path;
            string outputPath;

            try
            {
                path = args[0];
                outputPath = args[1];
                if (args.Length > 2) encoding = Encoding.GetEncoding(int.Parse(args[2]));
            }
            catch (Exception)
            {
                Usage();
                return;
            }


            var qdb = LoadDatabase(path, encoding);

            var questions = qdb.GetElementsByTagName("Q_DATA")
                .Cast<XmlNode>()
                .Select(Question.FromXml).ToList();


            var outputStrings = questions
                .Select(q => string.Format("{0} <b>({1})</b>", 
                    q.Text, string.Join(", ", q.Answers.Where(a => a.Correct).Select(a => a.Text))))
                .ToArray();

            File.WriteAllText(outputPath, CreateHtml(outputStrings), encoding);

        }
    }
}
